home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c / 296 < prev    next >
Internet Message Format  |  1996-08-06  |  6KB

  1. Path: newshost.lanl.gov!tanmoy
  2. From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
  3. Newsgroups: comp.lang.c,comp.lang.c++,gnu.gcc.help,gnu.g++.help,comp.os.msdos.djgpp,comp.std.c
  4. Subject: Re: float != float and floats as return types
  5. Date: 03 Feb 1996 17:56:44 GMT
  6. Organization: Los Alamos National Laboratory
  7. Message-ID: <TANMOY.96Feb3105644@qcd.lanl.gov>
  8. References: <4ej9lb$mpc@fu-berlin.de> <4elnjj$er4@server2.rz.uni-leipzig.de>
  9.     <4eqc7l$ugh@godzilla.zeta.org.au> <4eqtu3$ddo@der.twinsun.com>
  10.     <TANMOY.96Feb1164022@qcd.lanl.gov> <4ettr4$al5@der.twinsun.com>
  11. NNTP-Posting-Host: qcd.lanl.gov
  12. Mime-Version: 1.0
  13. Content-Type: text
  14. In-reply-to: eggert@twinsun.com's message of 2 Feb 1996 12:57:08 -0800
  15.  
  16.  
  17. Note that I have added comp.std.c to the list of newsgroups. 
  18.  
  19. In article <4ettr4$al5@der.twinsun.com> eggert@twinsun.com (Paul
  20. Eggert) writes: 
  21.    tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya) writes:
  22.  
  23.    > I do not agree with this interpretation, as the footnote (technically,
  24.    > not part of the standard) clarifies: an assignment and cast both must
  25.    > truncate the result to the required precision.
  26.  
  27.    Sorry, that footnote doesn't clarify matters.  All the footnote says
  28.    is that the required conversions must be performed.  In the example
  29.    we're talking about, there's no conversion to be performed, since
  30.    both sides of the assignment have `float' type.
  31.  
  32. There are two issues to be considered when interpreting the
  33. standard. In comp.std.c, the only concern is what the standard says
  34. and whether this is reasonable and consistent. In other discussions,
  35. including discussions of concrete implementations, I would guess what
  36. is important is also what the committee was trying to say, even if
  37. they did muddle up the issue. In this particular instance, the
  38. footnote would be completely redundant if the committee did not want
  39. the extra precision to be truncated. (At least, I cannot see any
  40. reason to put in the footnote otherwise.)
  41.  
  42.    Furthermore, even if there is a conversion, I don't see where the standard
  43.    requires that excess precision be discarded.  All the standard requires is
  44.    that the result be ``represented exactly''; it doesn't say what it means
  45.    by this, and an implementation that keeps excess precision is certainly
  46.    representing _its_ result exactly.  The standard explicitly licenses
  47.    excess precision, and never explicitly (or implicitly, as far as I can
  48.    see) requires excess precision to be discarded.
  49.  
  50.    Also please see ANSI C 3.3.4, which says ``A cast that specifies no
  51.    conversion has no effect on the type or value of the expression'';
  52.    this means that an implementation with extra precision is not allowed
  53.    to discard it because of a cast of float to float.  It is natural to
  54.    assume that assignment of float to float can behave similarly to a cast.
  55.  
  56. As you explicitly state, this is a conversion from float to float, so
  57. no conversion ought to take place according to the section
  58. quoted. This, I believe, needs to be corrected: I guess the
  59. possibility of extra precision was not thought of in this section. On
  60. the other hand, a conversion from double to float explicitly says
  61. (6.2.1.4) that the result is representable as a float exactly ... and
  62. quantities with extra precision is not. Hence, (float)(1.0/3.0) must
  63. be a value representable in a float (and I guess, representable is
  64. according to the guidelines in <float.h>), and, practically, it makes
  65. little sense to have (float)(x/y) have more precision if x and y are
  66. floats than if they were double or long double!
  67.  
  68. Furthermore, in an expression like (a=b*c)+d, the semantics is
  69. explicitly specified in the standard. (b*c) may be evaluated in
  70. extended precision, the result stored into a, the new stored value of
  71. a is then to be added to d. As one is assuming that the stored value
  72. of a cannot hold the extra precision, the quantity added to d must be
  73. the truncated value. Hence, b*c+d can be different from (a=b*c)+d.
  74.  
  75. It would not be practically useful to have (a=b*c)+d truncate whereas
  76. (float)(b*c) + d not truncate. I think, that unless the committee
  77. speaks otherwise, one can assume that cast and assignments indeed are
  78. intended to truncate.
  79.  
  80.    I realize that the standard is muddled in this area -- for example, the
  81.    Rationale contradicts it, and I've been told that the C Standard committee
  82.    may amend 3.3.4 because of the problem with floating point casts
  83.    (surely an implementation should be allowed to discard excess precision
  84.    whenever it wants to).
  85.  
  86.    That being the case, I think it wise for users not to rely on the fine
  87.    points here, and my advice to the GCC implementers is to hold off on
  88.    changing GCC's behavior until the C Standard committee acts.
  89.  
  90. I, on the other hand, think that the current standard  ought to be
  91. interpreted to mean that truncation is needed. Claiming that it is not
  92. needed is relying on the fine points. That is purely a personal
  93. opinion. 
  94.  
  95. The counter opinion that not truncating should be permitted on grounds
  96. of efficiency, I believe is a red herring. The C standard has already
  97. been written such that many useful optimizations cannot usually be
  98. performed by the compiler on floating expressions: a compiler is not
  99. free to rewrite a+b+c as a+(b+c) anyway (if a conforming program can
  100. detect the change, that is). For numerical work, one needs a flag
  101. which specifies standard-violating optimizations: or user coding of
  102. the exact transformations needed: with common subexpression
  103. recognition. Not truncating in the standard conforming mode is
  104. therefore neither useful nor necesary.
  105.  
  106. Cheers
  107. Tanmoy
  108. --
  109. tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
  110. Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
  111. Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
  112. <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
  113. internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
  114. fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
  115.